from rest_framework import viewsets, permissions
from ..models import DocumentVersion
from ..serializers import DocumentVersionSerializer

class DocumentVersionViewSet(viewsets.ModelViewSet):
    """文档版本视图集"""
    queryset = DocumentVersion.objects.all()
    serializer_class = DocumentVersionSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    def get_queryset(self):
        """获取文档版本列表"""
        document_id = self.request.query_params.get('document', None)
        if document_id:
            return DocumentVersion.objects.filter(document_id=document_id)
        return DocumentVersion.objects.none()

    def perform_create(self, serializer):
        """创建文档版本时自动设置创建者"""
        serializer.save(created_by=self.request.user) 